package com.wokeMy.view.channe.zhangcaitong;

import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.MybankblindActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_bindcarlist;
import com.woke.data.UnionPayChannel;
import com.woke.method.MyApp;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.html.YinZctHtmlActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.model.YlianZctPay;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class SeletCardActivity extends BaseActivity implements View.OnClickListener {

    TextView money_seletcard_tv,zfcard_seletcard,jscard_seletcard;
    RelativeLayout zhifu_selectcard_rl;
    RelativeLayout jieshuan_selectcard_rl;
    ImageView back;
    Button paybt;

    Data_bindcarlist listdata;
    Intent intent;
    float money;
    int way_id,vip_level;
    String user_id;
    String pay_card_no,card_no;
    UnionPayChannel unionPayChannel;
    Dialog dialog;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_selet_card);
    }

    @Override
    public void initActivity() {
        zhifu_selectcard_rl.setOnClickListener(this);
        jieshuan_selectcard_rl.setOnClickListener(this);
        back.setOnClickListener(this);
        paybt.setOnClickListener(this);
        money_seletcard_tv.setText("￥"+money);
    }

    @Override
    public void initView() {
        money_seletcard_tv = myFindViewById(R.id.money_seletcard_tv);
        zhifu_selectcard_rl = myFindViewById(R.id.zhifu_selectcard_rl);
        jieshuan_selectcard_rl = myFindViewById(R.id.jieshuan_selectcard_rl);
        zfcard_seletcard = myFindViewById(R.id.zfcard_seletcard);
        jscard_seletcard = myFindViewById(R.id.jscard_seletcard);
        back = myFindViewById(R.id.back_selectc_iv);
        paybt = myFindViewById(R.id.pay_selectc_bt);
    }

    @Override
    public void initData() {
        intent=getIntent();
        money = intent.getFloatExtra ("money", 0.01f);
        way_id = intent.getIntExtra ("way_id", 0);
//        user_id = intent.getStringExtra("user_id");
        user_id=((MyApp)getApplication()).getDatas_load().getId();
        vip_level=(Integer.parseInt(((MyApp)getApplication()).getDatas_load().getUser_type()));
//        unionPayChannel=(UnionPayChannel)intent.getSerializableExtra("unionPayChanne");
//        Log.e("initData",money+" "+unionPayChannel.getId()+" "+unionPayChannel.get)
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.money_seletcard_tv:
                break;
            case R.id.back_selectc_iv:
                finish();
                break;
            case R.id.pay_selectc_bt:
                if (pay_card_no==null||pay_card_no.equals("")){
                Toast.makeText(SeletCardActivity.this,"支付卡未选择",Toast.LENGTH_SHORT).show();
                }else if(card_no==null||card_no.equals("")){
                Toast.makeText(SeletCardActivity.this,"结算卡未选择",Toast.LENGTH_SHORT).show();
                }else {
                    dialog=  Util.createLoadingDialog(SeletCardActivity.this);
                    zctPay();
                }
//                Toast.makeText(SeletCardActivity.this,"暂未开通",Toast.LENGTH_SHORT).show();
                break;
            case R.id.zhifu_selectcard_rl:
                Intent intent = new Intent (SeletCardActivity.this, MybankblindActivity.class);
                intent.putExtra ("czhiortxian", "zhifucard");
                startActivityForResult (intent, Constant.REQUEST_ZHIFU);
                break;
            case R.id.jieshuan_selectcard_rl:
                Intent intent2 = new Intent (SeletCardActivity.this, MybankblindActivity.class);
                intent2.putExtra ("czhiortxian", "zscard");
                startActivityForResult (intent2, Constant.REQUEST_JIESHUAN);
                break;
            default:
                break;
        }
    }

    private void zctPay() {
        if (listdata!=null&&card_no!=null){
            Log.e("onActivityResult",money+ " "+way_id+" "+user_id+" "+pay_card_no+" "+card_no+" "+vip_level);

        }
//        if (way_id==73){
//            if (money>50000){
//               Toast.makeText(SeletCardActivity.this,"金额不大于5万",Toast.LENGTH_LONG).show();
//            }
//        }
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("amount", Double.parseDouble(String.valueOf(money)));
        params.put("way",way_id);
        params.put("pay_card_no",pay_card_no);
        params.put("card_no",card_no);
        params.put("vip_level",vip_level);
        Log.e("zctPay",user_id+" "+money+" "+way_id+" "+pay_card_no+" "+card_no+" "+" "+vip_level);
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.YINLZCT_URL, params, new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(dialog);
                //判断状态码
                if(i == 200){
                    //获取结果
                        if (way_id==73) {//掌财通
                            try {
                                String result = new String(bytes, "utf-8");
                                //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                                Log.e("zctPay/////////////////", result);
                                try {
                                    JSONObject jsonObject = new JSONObject(result);
                                    Log.e("zctPay", "jsonObject" + result);
                                    if (jsonObject.get("flag").equals("success")) {
                                        Gson gson = new Gson();
                                        java.lang.reflect.Type type = new TypeToken<YlianZctPay>() {
                                        }.getType();
                                        YlianZctPay ylianZctPay = gson.fromJson(result, type);
                                        Intent intent = new Intent(SeletCardActivity.this, YinZctHtmlActivity.class);
                                        intent.putExtra("url", ylianZctPay.getData().getData().getTranstr());
                                        startActivity(intent);
                                    } else {
                                        if (jsonObject.getString("info").equals("操作失败")) {
                                            if (jsonObject.getString("data")==null||jsonObject.getString("data").equals("")){
                                                Toast.makeText(SeletCardActivity.this, jsonObject.getString("info"), Toast.LENGTH_LONG).show();

                                            }else {
                                                String code=jsonObject.getJSONObject("data").getJSONObject("data").getString("code");
                                                    if(code.equals("888888")){
                                                        tishi("抱歉，该通道今日额度已用完，请选择其他通道！");
                                                 }else {
                                                        String message = jsonObject.getJSONObject("data").getJSONObject("data").getString("message");
                                                        tishi(message);
                                                 }

                                            }

                                        } else {
//                                            Toast.makeText(SeletCardActivity.this, jsonObject.getString("info"), Toast.LENGTH_LONG).show();
                                            tishi(jsonObject.getString("info"));
                                        }

                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            } catch (UnsupportedEncodingException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        Log.e("RESPONSE_JIESHUAN",resultCode+"");
        if (resultCode==Constant.RESPONSE_ZHIFU){
            listdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            pay_card_no=listdata.cardno;
            zfcard_seletcard.setText(pay_card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+pay_card_no.substring(pay_card_no.length()-3,pay_card_no.length()));
        }else if(resultCode==Constant.RESPONSE_JIESHUAN){
            listdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            card_no=listdata.cardno;
            jscard_seletcard.setText(card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+card_no.substring(card_no.length()-3,card_no.length()));
        }
    }

}
